<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1">
<title>A Java EE Application That Uses the JMS API with an Entity - The Java EE 5 Tutorial</title>
<meta name="robots" content="index,follow">
<meta name="robots" content="index,follow">
<meta name="date" content="2008-10-01">
<link rel="stylesheet" type="text/css" href="css/default.css">
<link rel="stylesheet" type="text/css" href="css/ipg.css">
<link rel="stylesheet" type="text/css" href="css/j5eetutorial.css">
</head>

<body>

<table border="0" cellpadding="5" cellspacing="0" width="100%">
<tbody>
   <tr valign="top">
      <td><p class="toc level1"><a href="docinfo.html">Document Information</a></p>
<p class="toc level1 tocsp"><a href="gexaf.html">Preface</a></p>
<p class="toc level1 tocsp"><a href="gfirp.html">Part&nbsp;I&nbsp;Introduction</a></p>
<p class="toc level2"><a href="bnaaw.html">1.&nbsp;&nbsp;Overview</a></p>
<p class="toc level2"><a href="gfiud.html">2.&nbsp;&nbsp;Using the Tutorial Examples</a></p>
<p class="toc level1 tocsp"><a href="bnadp.html">Part&nbsp;II&nbsp;The Web Tier</a></p>
<p class="toc level2"><a href="bnadr.html">3.&nbsp;&nbsp;Getting Started with Web Applications</a></p>
<p class="toc level2"><a href="bnafd.html">4.&nbsp;&nbsp;Java Servlet Technology</a></p>
<p class="toc level2"><a href="bnagx.html">5.&nbsp;&nbsp;JavaServer Pages Technology</a></p>
<p class="toc level2"><a href="bnajo.html">6.&nbsp;&nbsp;JavaServer Pages Documents</a></p>
<p class="toc level2"><a href="bnakc.html">7.&nbsp;&nbsp;JavaServer Pages Standard Tag Library</a></p>
<p class="toc level2"><a href="bnalj.html">8.&nbsp;&nbsp;Custom Tags in JSP Pages</a></p>
<p class="toc level2"><a href="bnaon.html">9.&nbsp;&nbsp;Scripting in JSP Pages</a></p>
<p class="toc level2"><a href="bnaph.html">10.&nbsp;&nbsp;JavaServer Faces Technology</a></p>
<p class="toc level2"><a href="bnaqz.html">11.&nbsp;&nbsp;Using JavaServer Faces Technology in JSP Pages</a></p>
<p class="toc level2"><a href="bnatx.html">12.&nbsp;&nbsp;Developing with JavaServer Faces Technology</a></p>
<p class="toc level2"><a href="bnavg.html">13.&nbsp;&nbsp;Creating Custom UI Components</a></p>
<p class="toc level2"><a href="bnawo.html">14.&nbsp;&nbsp;Configuring JavaServer Faces Applications</a></p>
<p class="toc level2"><a href="bnaxu.html">15.&nbsp;&nbsp;Internationalizing and Localizing Web Applications</a></p>
<p class="toc level1 tocsp"><a href="bnayk.html">Part&nbsp;III&nbsp;Web Services</a></p>
<p class="toc level2"><a href="bnayl.html">16.&nbsp;&nbsp;Building Web Services with JAX-WS</a></p>
<p class="toc level2"><a href="bnazf.html">17.&nbsp;&nbsp;Binding between XML Schema and Java Classes</a></p>
<p class="toc level2"><a href="bnbdv.html">18.&nbsp;&nbsp;Streaming API for XML</a></p>
<p class="toc level2"><a href="bnbhf.html">19.&nbsp;&nbsp;SOAP with Attachments API for Java</a></p>
<p class="toc level1 tocsp"><a href="bnblr.html">Part&nbsp;IV&nbsp;Enterprise Beans</a></p>
<p class="toc level2"><a href="bnbls.html">20.&nbsp;&nbsp;Enterprise Beans</a></p>
<p class="toc level2"><a href="bnbnb.html">21.&nbsp;&nbsp;Getting Started with Enterprise Beans</a></p>
<p class="toc level2"><a href="bnboc.html">22.&nbsp;&nbsp;Session Bean Examples</a></p>
<p class="toc level2"><a href="bnbpk.html">23.&nbsp;&nbsp;A Message-Driven Bean Example</a></p>
<p class="toc level1 tocsp"><a href="bnbpy.html">Part&nbsp;V&nbsp;Persistence</a></p>
<p class="toc level2"><a href="bnbpz.html">24.&nbsp;&nbsp;Introduction to the Java Persistence API</a></p>
<p class="toc level2"><a href="bnbrl.html">25.&nbsp;&nbsp;Persistence in the Web Tier</a></p>
<p class="toc level2"><a href="bnbrs.html">26.&nbsp;&nbsp;Persistence in the EJB Tier</a></p>
<p class="toc level2"><a href="bnbtg.html">27.&nbsp;&nbsp;The Java Persistence Query Language</a></p>
<p class="toc level1 tocsp"><a href="bnbwi.html">Part&nbsp;VI&nbsp;Services</a></p>
<p class="toc level2"><a href="bnbwj.html">28.&nbsp;&nbsp;Introduction to Security in the Java EE Platform</a></p>
<p class="toc level2"><a href="bnbyk.html">29.&nbsp;&nbsp;Securing Java EE Applications</a></p>
<p class="toc level2"><a href="bncas.html">30.&nbsp;&nbsp;Securing Web Applications</a></p>
<p class="toc level2"><a href="bncdq.html">31.&nbsp;&nbsp;The Java Message Service API</a></p>
<p class="toc level2"><a href="bncgv.html">32.&nbsp;&nbsp;Java EE Examples Using the JMS API</a></p>
<p class="toc level3"><a href="bncgw.html">A Java EE Application That Uses the JMS API with a Session Bean</a></p>
<p class="toc level4"><a href="bncgw.html#bncgx">Writing the Application Components for the <tt>clientsessionmdb</tt> Example</a></p>
<p class="toc level5"><a href="bncgw.html#bncgz">Coding the Application Client: <tt>MyAppClient.java</tt></a></p>
<p class="toc level5"><a href="bncgw.html#bncha">Coding the Publisher Session Bean</a></p>
<p class="toc level5"><a href="bncgw.html#bnchb">Coding the Message-Driven Bean: <tt>MessageBean.java</tt></a></p>
<p class="toc level4 tocsp"><a href="bncgw.html#bnchc">Creating Resources for the <tt>clientsessionmdb</tt> Example</a></p>
<p class="toc level4"><a href="bncgw.html#bnchd">Building, Deploying, and Running the <tt>clientsessionmdb</tt> Example Using NetBeans IDE</a></p>
<p class="toc level4"><a href="bncgw.html#bnche">Building, Deploying, and Running the <tt>clientsessionmdb</tt> Example Using Ant</a></p>
<div class="onpage">
<p class="toc level3 tocsp"><a href="">A Java EE Application That Uses the JMS API with an Entity</a></p>
<p class="toc level4"><a href="#bnchg">Overview of the <tt>clientmdbentity</tt> Example Application</a></p>
<p class="toc level4"><a href="#bnchi">Writing the Application Components for the <tt>clientmdbentity</tt> Example</a></p>
<p class="toc level5"><a href="#bnchj">Coding the Application Client: <tt>HumanResourceClient.java</tt></a></p>
<p class="toc level5"><a href="#bnchk">Coding the Message-Driven Beans for the <tt>clientmdbentity</tt> Example</a></p>
<p class="toc level5"><a href="#bnchl">Coding the Entity Class for the <tt>clientmdbentity</tt> Example</a></p>
<p class="toc level4 tocsp"><a href="#bnchm">Creating Resources for the <tt>clientmdbentity</tt> Example</a></p>
<p class="toc level4"><a href="#bnchn">Building, Deploying, and Running the <tt>clientmdbentity</tt> Example Using NetBeans IDE</a></p>
<p class="toc level4"><a href="#bncho">Building, Deploying, and Running the <tt>clientmdbentity</tt> Example Using Ant</a></p>
</div>
<p class="toc level3 tocsp"><a href="bnchp.html">An Application Example That Consumes Messages from a Remote Server</a></p>
<p class="toc level4"><a href="bnchp.html#bnchq">Overview of the <tt>consumeremote</tt> Example Modules</a></p>
<p class="toc level4"><a href="bnchp.html#bnchs">Writing the Module Components for the <tt>consumeremote</tt> Example</a></p>
<p class="toc level4"><a href="bnchp.html#bncht">Creating Resources for the <tt>consumeremote</tt> Example</a></p>
<p class="toc level4"><a href="bnchp.html#bnchu">Using Two Application Servers for the <tt>consumeremote</tt> Example</a></p>
<p class="toc level4"><a href="bnchp.html#bnchv">Building, Deploying, and Running the <tt>consumeremote</tt>Modules Using NetBeans IDE</a></p>
<p class="toc level4"><a href="bnchp.html#bnchw">Building, Deploying, and Running the <tt>consumeremote</tt> Modules Using Ant</a></p>
<p class="toc level3 tocsp"><a href="bnchx.html">An Application Example That Deploys a Message-Driven Bean on Two Servers</a></p>
<p class="toc level4"><a href="bnchx.html#bnchy">Overview of the <tt>sendremote</tt> Example Modules</a></p>
<p class="toc level4"><a href="bnchx.html#bnchz">Writing the Module Components for the <tt>sendremote</tt> Example</a></p>
<p class="toc level5"><a href="bnchx.html#bncib">Coding the Application Client: <tt>MultiAppServerClient.java</tt></a></p>
<p class="toc level5"><a href="bnchx.html#bncic">Coding the Message-Driven Bean: <tt>ReplyMsgBean.java</tt></a></p>
<p class="toc level4 tocsp"><a href="bnchx.html#bncid">Creating Resources for the <tt>sendremote</tt> Example</a></p>
<p class="toc level4"><a href="bnchx.html#bncie">Using Two Application Servers for the <tt>sendremote</tt> Example</a></p>
<p class="toc level4"><a href="bnchx.html#bncif">Building, Deploying, and Running the <tt>sendremote</tt> Modules Using NetBeans IDE</a></p>
<p class="toc level4"><a href="bnchx.html#bncig">Building, Deploying, and Running the <tt>sendremote</tt> Modules Using Ant</a></p>
<p class="toc level2 tocsp"><a href="bncih.html">33.&nbsp;&nbsp;Transactions</a></p>
<p class="toc level2"><a href="bncjh.html">34.&nbsp;&nbsp;Resource Connections</a></p>
<p class="toc level2"><a href="bncjx.html">35.&nbsp;&nbsp;Connector Architecture</a></p>
<p class="toc level1 tocsp"><a href="bnckn.html">Part&nbsp;VII&nbsp;Case Studies</a></p>
<p class="toc level2"><a href="bncko.html">36.&nbsp;&nbsp;The Coffee Break Application</a></p>
<p class="toc level2"><a href="bnclz.html">37.&nbsp;&nbsp;The Duke's Bank Application</a></p>
<p class="toc level1 tocsp"><a href="gexbq.html">Part&nbsp;VIII&nbsp;Appendixes</a></p>
<p class="toc level2"><a href="bncno.html">A.&nbsp;&nbsp;Java Encoding Schemes</a></p>
<p class="toc level2"><a href="bncnq.html">B.&nbsp;&nbsp;Preparation for Java EE Certification Exams</a></p>
<p class="toc level2"><a href="bncnt.html">C.&nbsp;&nbsp;About the Authors</a></p>
<p class="toc level1 tocsp"><a href="idx-1.html">Index</a></p>
</td>
      <td width="10px">&nbsp;</td>
      <td width="705px">
         <div class="header">
             <div class="header-links-top">
                 <a href="http://java.sun.com">java.sun.com</a> |
                 <a href="http://docs.sun.com/">docs.sun.com</a><br>
             </div> 
             <img src="graphics/tutorialBanner.gif" width="704" height="120" alt="The Java&trade; EE 5 Tutorial"/>
             <div class="header-links">
	         <a href="index.html">Home</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/information/download.html">Download</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/JavaEETutorial.pdf">PDF</a> |
                 <a href="http://java.sun.com/javaee/5/docs/api/index.html">API</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/information/faq.html">FAQ</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/information/search.html">Search</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/information/sendusmail.html">Feedback</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/information/history.html">History</a>
             </div>
             <div class="navigation">
                 <a href="bncgw.html"><img style="padding-right: 3px" src="graphics/leftButton.gif" border="0"></a>
                 <a href="sjsaseej2eet.html"><img style="padding-right: 3px" src="graphics/upButton.gif" border="0"></a>
                 <a href="bnchp.html"><img style="padding-left: 3px" src="graphics/rightButton.gif" border="0"></a>
             </div>
         </div>

	 <div class="maincontent">      	 
             

<a name="bnchf"></a><h3>A Java EE Application That Uses the JMS API with an Entity</h3>
<a name="indexterm-2977"></a><a name="indexterm-2978"></a><a name="indexterm-2979"></a><a name="indexterm-2980"></a><a name="indexterm-2981"></a><p>This section explains how to write, compile, package, deploy, and run a Java
EE application that uses the JMS API with an entity. The application uses
the following components:</p>
<ul><li><p>An application client that both sends and receives messages</p></li>
<li><p>Two message-driven beans</p></li>
<li><p>An entity class</p></li></ul>
<p>This section covers the following topics:</p>
<ul><li><p><a href="#bnchg">Overview of the <tt>clientmdbentity</tt> Example Application</a></p></li>
<li><p><a href="#bnchi">Writing the Application Components for the <tt>clientmdbentity</tt> Example</a></p></li>
<li><p><a href="#bnchm">Creating Resources for the <tt>clientmdbentity</tt> Example</a></p></li>
<li><p><a href="#bnchn">Building, Deploying, and Running the <tt>clientmdbentity</tt> Example Using NetBeans IDE</a></p></li>
<li><p><a href="#bncho">Building, Deploying, and Running the <tt>clientmdbentity</tt> Example Using Ant</a></p></li></ul>
<p>You will find the source files for this section in the directory
<tt></tt><i>tut-install</i><tt>/javaeetutorial5/examples/jms/clientmdbentity/.</tt> Path names in this section are relative to this directory.</p>

<a name="bnchg"></a><h4>Overview of the <tt>clientmdbentity</tt> Example Application</h4>
<p>This application simulates, in a simplified way, the work flow of a company&rsquo;s
human resources (HR) department when it processes a new hire. This application also
demonstrates how to use the Java EE platform to accomplish a task that
many JMS client applications perform.</p><p>A JMS client must often wait for several messages from various sources. It
then uses the information in all these messages to assemble a message
that it then sends to another destination. The common term for this process
is <b>joining messages</b>. Such a task must be transactional, with all the receives and
the send as a single transaction. If not all the messages are received
successfully, the transaction can be rolled back. For a client example that illustrates
this task, see <a href="bncfu.html#bncgj">A Local Transaction Example</a>.</p><p>A message-driven bean can process only one message at a time in
a transaction. To provide the ability to join messages, a Java EE application
can have the message-driven bean store the interim information in an entity. The
entity can then determine whether all the information has been received; when it has,
the entity can report this back to one of the message-driven beans,
which then creates and sends the message to the other destination. After it
has completed its task, the entity can be removed.</p><p>The basic steps of the application are as follows.</p>
<ol><li><p>The HR department&rsquo;s application client generates an employee ID for each new hire and then publishes a message (M1) containing the new hire&rsquo;s name, employee ID, and position. The client then creates a temporary queue, <tt>ReplyQueue</tt>, with a message listener that waits for a reply to the message. (See <a href="bncfu.html#bncgb">Creating Temporary Destinations</a>, for more information.)</p></li>
<li><p>Two message-driven beans process each message: One bean, <tt>OfficeMDB</tt>, assigns the new hire&rsquo;s office number, and the other bean, <tt>EquipmentMDB</tt>, assigns the new hire&rsquo;s equipment. The first bean to process the message creates and persists an entity named <tt>SetupOffice</tt>, then calls a business method of the entity to store the information it has generated. The second bean locates the existing entity and calls another business method to add its information.</p></li>
<li><p>When both the office and the equipment have been assigned, the entity business method returns a value of <tt>true</tt> to the message-driven bean that called the method. The message-driven bean then sends to the reply queue a message (M2) describing the assignments. Then it removes the entity. The application client&rsquo;s message listener retrieves the information.</p></li></ol>
<p><a href="#bnchh">Figure&nbsp;32-2</a> illustrates the structure of this application. Of course, an actual HR application
would have more components; other beans could set up payroll and benefits records, schedule
orientation, and so on.</p><p><a href="#bnchh">Figure&nbsp;32-2</a> assumes that <tt>OfficeMDB</tt> is the first message-driven bean to consume the
message from the client. <tt>OfficeMDB</tt> then creates and persists the <tt>SetupOffice</tt> entity and
stores the office information. <tt>EquipmentMDB</tt> then finds the entity, stores the equipment
information, and learns that the entity has completed its work. <tt>EquipmentMDB</tt> then sends the
message to the reply queue and removes the entity.</p><a name="bnchh"></a><h6>Figure&nbsp;32-2 A Java EE Application: Client to Message-Driven Beans to Entity</h6><img src="figures/jmsj2ee-clientMdbEntityApp.gif" alt="Diagram of application showing an application client, two message-driven beans, and an entity"></img>

<a name="bnchi"></a><h4>Writing the Application Components for the <tt>clientmdbentity</tt> Example</h4>
<p>Writing the components of the application involves the following:</p>
<ul><li><p><a href="#bnchj">Coding the Application Client: <tt>HumanResourceClient.java</tt></a></p></li>
<li><p><a href="#bnchk">Coding the Message-Driven Beans for the <tt>clientmdbentity</tt> Example</a></p></li>
<li><p><a href="#bnchl">Coding the Entity Class for the <tt>clientmdbentity</tt> Example</a></p></li></ul>


<a name="bnchj"></a><h5>Coding the Application Client: <tt>HumanResourceClient.java</tt></h5>
<a name="indexterm-2982"></a><a name="indexterm-2983"></a><a name="indexterm-2984"></a><a name="indexterm-2985"></a><p>The application client program, <tt>clientmdbentity-app-client/src/java/HumanResourceClient.java</tt>, performs the following steps:</p>
<ol><li><p>Injects <tt>ConnectionFactory</tt> and <tt>Topic</tt> resources</p></li>
<li><p>Creates a <tt>TemporaryQueue</tt> to receive notification of processing that occurs, based on new-hire events it has published</p></li>
<li><p>Creates a <tt>MessageConsumer</tt> for the <tt>TemporaryQueue</tt>, sets the <tt>MessageConsumer</tt>&rsquo;s message listener, and starts the connection</p></li>
<li><p>Creates a <tt>MessageProducer</tt> and a <tt>MapMessage</tt></p></li>
<li><p>Creates five new employees with randomly generated names, positions, and ID numbers (in sequence) and publishes five messages containing this information</p></li></ol>
<p>The message listener, <tt>HRListener</tt>, waits for messages that contain the assigned office and
equipment for each employee. When a message arrives, the message listener displays the
information received and determines whether all five messages have arrived. When they have,
the message listener notifies the main program, which then exits.</p>

<a name="bnchk"></a><h5>Coding the Message-Driven Beans for the <tt>clientmdbentity</tt> Example</h5>
<a name="indexterm-2986"></a><p>This example uses two message-driven beans:</p>
<ul><li><p><tt>clientmdbentity-ejb/src/java/EquipmentMDB.java</tt></p></li>
<li><p><tt>clientmdbentity-ejb/src/java/OfficeMDB.java</tt></p></li></ul>
<p>The beans take the following steps:</p>
<ol><li><p>They inject <tt>MessageDrivenContext</tt> and <tt>ConnectionFactory</tt> resources.</p></li>
<li><p>The <tt>onMessage</tt> method retrieves the information in the message. The <tt>EquipmentMDB</tt>&rsquo;s <tt>onMessage</tt> method chooses equipment, based on the new hire&rsquo;s position; the <tt>OfficeMDB</tt>&rsquo;s <tt>onMessage</tt> method randomly generates an office number.</p></li>
<li><p>After a slight delay to simulate real world processing hitches, the <tt>onMessage</tt> method calls a helper method, <tt>compose</tt>.</p></li>
<li><p>The <tt>compose</tt> method takes the following steps:</p>
<ol><li><p>It either creates and persists the <tt>SetupOffice</tt> entity or finds it by primary key.</p></li>
<li><p>It uses the entity to store the equipment or the office information in the database, calling either the <tt>doEquipmentList</tt> or the <tt>doOfficeNumber</tt> business method.</p></li>
<li><p>If the business method returns <tt>true</tt>, meaning that all of the information has been stored, it creates a connection and a session, retrieves the reply destination information from the message, creates a <tt>MessageProducer</tt>, and sends a reply message that contains the information stored in the entity.</p></li>
<li><p>It removes the entity.</p></li></ol>
</li></ol>


<a name="bnchl"></a><h5>Coding the Entity Class for the <tt>clientmdbentity</tt> Example</h5>
<p>The <tt>SetupOffice</tt> class, <tt>SetupOffice.java</tt>, is an entity class. The entity and the message-driven
beans are packaged together in an EJB JAR file. The entity class is
declared as follows:</p><pre>@Entity
public class SetupOffice implements Serializable {</pre><p>The class contains a no-argument constructor and a constructor that takes two arguments,
the employee ID and name. It also contains getter and setter methods for
the employee ID, name, office number, and equipment list. The getter method for
the employee ID has the <tt>@Id</tt> annotation to indicate that this field is
the primary key:</p><pre>@Id public String getEmployeeId() {
    return id;
}</pre><p>The class also implements the two business methods, <tt>doEquipmentList</tt> and <tt>doOfficeNumber</tt>, and
their helper method, <tt>checkIfSetupComplete</tt>.</p><p>The message-driven beans call the business methods and the getter methods.</p><p>The <tt>persistence.xml</tt> file for the entity specifies the most basic settings:</p><pre>&lt;persistence>
    &lt;persistence-unit name="clientmdbentity">
        &lt;jta-data-source>jdbc/__default&lt;/jta-data-source>
        &lt;class>eb.SetupOffice&lt;/class>
        &lt;properties>
            &lt;property name="toplink.ddl-generation"
                 value="drop-and-create-tables"/>
        &lt;/properties>
    &lt;/persistence-unit>
&lt;/persistence></pre>

<a name="bnchm"></a><h4>Creating Resources for the <tt>clientmdbentity</tt> Example</h4>
<p>This example uses the connection factory <tt>jms/ConnectionFactory</tt> and the topic <tt>jms/Topic</tt>, both of
which you used in <a href="bncgw.html">A Java EE Application That Uses the JMS API with a Session Bean</a>. It also uses the JDBC resource named
<tt>jdbc/__default</tt>, which is enabled by default when you start the Application Server.</p><p>If you deleted the connection factory or topic, you can create them again
using targets in the <tt>build.xml</tt> file for this example. Use the following commands
to create the resources:</p><pre><tt><b>ant create-cf ant create-topic</b></tt></pre>

<a name="bnchn"></a><h4>Building, Deploying, and Running the <tt>clientmdbentity</tt> Example Using NetBeans IDE</h4>
<p>To build, deploy, and run the application using NetBeans IDE, do the following:</p>
<ol><li><p>Start the Application Server, if it is not already running.</p></li>
<li><p>Start the database server as described in <a href="bnadk.html">Starting and Stopping the Java DB Database Server</a>, if it is not already running.</p></li>
<li><p>In NetBeans IDE, choose Open Project from the File menu.</p></li>
<li><p>In the Open Project dialog, navigate to <tt></tt><i>tut-install</i><tt>/javaeetutorial5/examples/jms/</tt>.</p></li>
<li><p>Select the <tt>clientmdbentity</tt> folder.</p></li>
<li><p>Select the Open as Main Project check box and the Open Required Projects check box.</p></li>
<li><p>Click Open Project.</p></li>
<li><p>Right-click the <tt>clientmdbentity</tt> project and choose Build.</p><p>This task creates the following:</p>
<ul><li><p>An application client JAR file that contains the client class and listener class files, along with a manifest file that specifies the main class</p></li>
<li><p>An EJB JAR file that contains the message-driven beans and the entity class, along with the <tt>persistence.xml</tt> file</p></li>
<li><p>An application EAR file that contains the two JAR files along with an <tt>application.xml</tt> file</p></li></ul>
</li>
<li><p>Right-click the project and choose Undeploy and Deploy.</p></li>
<li><p>Right-click the project and choose Run.</p><p>This command returns a JAR file named <tt>clientmdbentityClient.jar</tt> and then executes it.</p></li></ol>
<p>The output of the application client in the Output pane looks something like
this:</p><pre>PUBLISHER: Setting hire ID to 25, name Gertrude Bourbon, position Senior Programmer
PUBLISHER: Setting hire ID to 26, name Jack Verdon, position Manager
PUBLISHER: Setting hire ID to 27, name Fred Tudor, position Manager
PUBLISHER: Setting hire ID to 28, name Fred Martin, position Programmer
PUBLISHER: Setting hire ID to 29, name Mary Stuart, position Manager
Waiting for 5 message(s)
New hire event processed:
  Employee ID: 25
  Name: Gertrude Bourbon
  Equipment: Laptop
  Office number: 183
Waiting for 4 message(s)
New hire event processed:
  Employee ID: 26
  Name: Jack Verdon
  Equipment: Pager
  Office number: 20
Waiting for 3 message(s)
New hire event processed:
  Employee ID: 27
  Name: Fred Tudor
  Equipment: Pager
  Office number: 51
Waiting for 2 message(s)
New hire event processed:
  Employee ID: 28
  Name: Fred Martin
  Equipment: Desktop System
  Office number: 141
Waiting for 1 message(s)
New hire event processed:
  Employee ID: 29
  Name: Mary Stuart
  Equipment: Pager
  Office number: 238</pre><p>The output from the message-driven beans and the entity class appears in the
server log, wrapped in logging information.</p><p>For each employee, the application first creates the entity and then finds it.
You may see runtime errors in the server log, and transaction rollbacks may
occur. The errors occur if both of the message-driven beans discover at the
same time that the entity does not yet exist, so they both
try to create it. The first attempt succeeds, but the second fails because
the bean already exists. After the rollback, the second message-driven bean tries again
and succeeds in finding the entity. Container-managed transactions allow the application to run correctly,
in spite of these errors, with no special programming.</p><p>You can run the application client repeatedly.</p><p>Undeploy the application after you finish running the client. To undeploy the application,
follow these steps:</p>
<ol><li><p>Click the Services tab.</p></li>
<li><p>Expand the Servers node.</p></li>
<li><p>Expand the Sun Java System Application Server node.</p></li>
<li><p>Expand the Applications node.</p></li>
<li><p>Expand the Enterprise Applications node.</p></li>
<li><p>Right-click <tt>clientmdbentity</tt> and choose Undeploy.</p></li></ol>
<p>To remove the generated files, right-click the <tt>clientmdbentity</tt> project and choose Clean.</p>

<a name="bncho"></a><h4>Building, Deploying, and Running the <tt>clientmdbentity</tt> Example Using Ant</h4>
<p>To create and package the application using Ant, perform these steps:</p>
<ol><li><p>Start the Application Server, if it is not already running.</p></li>
<li><p>Start the database server as described in <a href="bnadk.html">Starting and Stopping the Java DB Database Server</a>.</p></li>
<li><p>Go to the following directory:</p><pre><tt></tt><i>tut-install</i><tt>/javaeetutorial5/examples/jms/clientmdbentity/</tt></pre></li>
<li><p>To compile the source files and package the application, use the following command:</p><pre><tt><b>ant</b></tt></pre></li></ol>
<p>The <tt>ant</tt> command creates the following:</p>
<ul><li><p>An application client JAR file that contains the client class and listener class files, along with a manifest file that specifies the main class</p></li>
<li><p>An EJB JAR file that contains the message-driven beans and the entity class, along with the <tt>persistence.xml</tt> file</p></li>
<li><p>An application EAR file that contains the two JAR files along with an <tt>application.xml</tt> file</p></li></ul>
<p>To deploy the application and run the client, use the following command:</p><pre><tt><b>ant run</b></tt></pre><p>Ignore the message that states that the application is deployed at a URL.</p><p>The program output in the terminal window looks something like this:</p><pre>running application client container.
PUBLISHER: Setting hire ID to 25, name Gertrude Bourbon, position Senior Programmer
PUBLISHER: Setting hire ID to 26, name Jack Verdon, position Manager
PUBLISHER: Setting hire ID to 27, name Fred Tudor, position Manager
PUBLISHER: Setting hire ID to 28, name Fred Martin, position Programmer
PUBLISHER: Setting hire ID to 29, name Mary Stuart, position Manager
Waiting for 5 message(s)
New hire event processed:
  Employee ID: 25
  Name: Gertrude Bourbon
  Equipment: Laptop
  Office number: 183
Waiting for 4 message(s)
New hire event processed:
  Employee ID: 26
  Name: Jack Verdon
  Equipment: Pager
  Office number: 20
Waiting for 3 message(s)
New hire event processed:
  Employee ID: 27
  Name: Fred Tudor
  Equipment: Pager
  Office number: 51
Waiting for 2 message(s)
New hire event processed:
  Employee ID: 28
  Name: Fred Martin
  Equipment: Desktop System
  Office number: 141
Waiting for 1 message(s)
New hire event processed:
  Employee ID: 29
  Name: Mary Stuart
  Equipment: Pager
  Office number: 238</pre><p>The output from the message-driven beans and the entity class appears in the
server log, wrapped in logging information.</p><p>For each employee, the application first creates the entity and then finds it.
You may see runtime errors in the server log, and transaction rollbacks may
occur. The errors occur if both of the message-driven beans discover at the
same time that the entity does not yet exist, so they both
try to create it. The first attempt succeeds, but the second fails because
the bean already exists. After the rollback, the second message-driven bean tries again
and succeeds in finding the entity. Container-managed transactions allow the application to run correctly,
in spite of these errors, with no special programming.</p><p>Undeploy the application after you finish running the client:</p><pre><tt><b>ant undeploy</b></tt></pre><p>To remove the generated files, use the following command in the <tt>clientmdbentity</tt>,
<tt>clientmdbentity-app-client</tt>, and <tt>clientmdbentity-ejb</tt> directories:</p><pre><tt><b>ant clean</b></tt></pre>
         </div>
         <div class="navigation">
             <a href="bncgw.html"><img style="padding-right: 3px" src="graphics/leftButton.gif" border="0"></a>
             <a href="sjsaseej2eet.html"><img style="padding-right: 3px" src="graphics/upButton.gif" border="0"></a>
             <a href="bnchp.html"><img style="padding-left: 3px" src="graphics/rightButton.gif" border="0"></a>
         </div>

         <div class="copyright">
      	    <p>The material in The Java&trade; EE 5 Tutorial is <a href='docinfo.html'>copyright</a>-protected and may not be published in other works without express written permission from Sun Microsystems.</p>
      	 </div>

      </td>
   </tr>
</tbody>
</table>
</body>
</html>

